import tkinter as tk
import random
import threading
import time
import sys   # 新增：用于强制退出进程

running = True          # 全局生死开关

def dow():
    """单窗口线程函数"""
    window = tk.Tk()
    width = window.winfo_screenwidth()
    height = window.winfo_screenheight()
    x = random.randrange(0, width - 220)   # 留一点边距
    y = random.randrange(0, height - 50)
    window.title('亲爱的')
    window.geometry(f"220x50+{x}+{y}")
    window.attributes('-topmost', True)    # 置顶，防止被挡住

    tk.Label(window,
             text="我想你了！",
             bg='pink',
             font=('楷体', 18),
             width=25, height=4).pack()

    # 定时检查全局开关，决定自己是否关闭
    def heartbeat():
        if not running:
            window.destroy()
        else:
            window.after(50, heartbeat)

    heartbeat()
    window.mainloop()

def killer():
    """8 秒后杀掉整个程序"""
    global running
    time.sleep(8)
    running = False        # 通知所有窗口
    time.sleep(0.5)        # 给窗口一点销毁时间
    sys.exit(0)            # 强制结束进程

# 启动定时杀手线程（守护线程）
threading.Thread(target=killer, daemon=True).start()

# 批量创建 200 个窗口线程
threads = []
for _ in range(200):
    t = threading.Thread(target=dow)
    threads.append(t)
    time.sleep(0.01)   # 稍微错开，降低瞬间压力
    t.start()